n=500;   %number of intervals
start_ind=1;
end_ind=351;
ind=end_ind-start_ind+1; %number of subjects in the analysis
cutoffs=zeros(ind,5); %output matrix for individual cutpoints, newid, c1, c2 and c3 and the number of correctly classified moves
s_min=0;    %minimal strength (0 in the experiment)        
s_max=1000; %maximal strength (1000 in the experiment)
actions = xlsread('actions.xls'); %all actions, first column are subjectid's, columns 2-41 are decisions in rounds 1-40. 1=flee, 2=wait&flee, 3=wait&fight, 4=fight, 5=wait and other moves, 9=no decision
abilities = xlsread('strengths.xls'); %all abilities, first column are subjectid's, columns 2-41 are abilities in rounds 1-40. 

for i=start_ind:end_ind
    scores=zeros(n+1,n+1,n+1); %output matrix for all the combinations of cutpoints
    
    for c1=0:n
    for c2=c1:n
    for c3=c2:n       
        interimscore=0;    
            for t=21:40
                if (abilities(i,t+1)<=(c1/n)*s_max) && (actions(i,t+1)==1)            
                interimscore=interimscore+1;
                elseif (abilities(i,t+1)>(c1/n)*s_max) && (abilities(i,t+1)<=(c2/n)*s_max) && (actions(i,t+1)==2) 
                interimscore=interimscore+1; 
                elseif (abilities(i,t+1)>(c1/n)*s_max) && (abilities(i,t+1)<=(c2/n)*s_max) && (actions(i,t+1)==5) 
                interimscore=interimscore+1;  
                elseif (abilities(i,t+1)>(c2/n)*s_max) && (abilities(i,t+1)<=(c3/n)*s_max) && (actions(i,t+1)==3) 
                interimscore=interimscore+1;  
                elseif (abilities(i,t+1)>(c2/n)*s_max) && (abilities(i,t+1)<=(c3/n)*s_max) && (actions(i,t+1)==5) 
                interimscore=interimscore+1;  
                elseif (abilities(i,t+1)>(c3/n)*s_max) && (actions(i,t+1)==4) 
                interimscore=interimscore+1;    
                else
                interimscore=interimscore+0;     
                end      
            end
        scores(c1+1,c2+1,c3+1)=interimscore;
    end
    end
    end
    [M,I]=max(scores(:));
    [I1,I2,I3] = ind2sub(size(scores),I);
    cutoffs(i-start_ind+1,1)=actions(i,1);
    cutoffs(i-start_ind+1,2)=(((I1-1)/n))*s_max;
    cutoffs(i-start_ind+1,3)=(((I2-1)/n))*s_max;
    cutoffs(i-start_ind+1,4)=(((I3-1)/n))*s_max;
    cutoffs(i-start_ind+1,5)=M(1,1,1);
    
end